package com.rabbiter.oes.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.MultiQuestion;
import org.apache.ibatis.annotations.*;

import java.util.List;

//选择题
@Mapper
public interface MultiQuestionMapper extends BaseMapper<MultiQuestion> {
    /**
     * select * from multiquestions where questionId in (
     * 	select questionId from papermanage where questionType = 1 and paperId = 1001
     * )
     */
    @Select("select * from multi_question where questionId in (select questionId from paper_manage where questionType = 1 and paperId = #{paperId} )")
    List<MultiQuestion> findByIdAndType(String PaperId);

    @Select("select * from multi_question")
    IPage<MultiQuestion> findAll(Page page);

    /**
     * 查询最后一条记录的questionId
     * @return MultiQuestion
     */
    @Select("select questionId from multi_question order by questionId desc limit 1")
    MultiQuestion findOnlyQuestionId();

//    @Options(useGeneratedKeys = true,keyProperty = "questionId")
//    @Insert("insert into multi_question(subject,question,answerA,answerB,answerC,answerD,rightAnswer,analysis,section,level) " +
//            "values(#{subject},#{question},#{answerA},#{answerB},#{answerC},#{answerD},#{rightAnswer},#{analysis},#{level})")
//    int add(MultiQuestion multiQuestion);
    @Options(useGeneratedKeys = true,keyProperty = "questionId")
    @Insert("insert into multi_question(subject,question,answerA,answerB,answerC,answerD,rightAnswer,score) " +
            "values(#{subject},#{question},#{answerA},#{answerB},#{answerC},#{answerD},#{rightAnswer},#{score})")
    int add(MultiQuestion multiQuestion);

    @Select("select questionId from multi_question  where subject =#{subject} order by rand() desc limit #{pageNo}")
    List<Integer> findBySubject(@Param("subject") String subject, @Param("pageNo") Integer pageNo);

    @Update("update multi_question set subject = #{subject}, question = #{question}, answerA = #{answerA}, answerB = #{answerB}, answerC = #{answerC}, answerD = #{answerD}, rightAnswer = #{rightAnswer}, analysis = #{analysis}, where questionId = #{questionId}")
    int edit(MultiQuestion multiQuestion);

    @Select("SELECT * FROM multi_question " +
            "WHERE subject = #{subject} " +
            "ORDER BY RAND() LIMIT #{count}")
    List<MultiQuestion> findRandom(
            @Param("count") Integer count,
            @Param("subject") String subject
    );

    @Select("select * from multi_question where subject = #{subject}")
    List<MultiQuestion> findSequence(String subject);

    @Select(" SELECT mq.* FROM multi_question mq INNER JOIN paper_manage pm ON mq.questionId = pm.questionId  WHERE pm.PaperId = #{PaperId} AND pm.userId = #{userId} AND pm.questionType = 1 ")
    List<MultiQuestion> findByExamCodeAndUserId(@Param("PaperId") String PaperId,
                                                @Param("userId") Long userId);
}
